package com.haowu.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haowu.common.core.constant.UserConstants;
import com.haowu.common.core.utils.StringUtils;
import com.haowu.common.mybatis.core.page.PageQuery;
import com.haowu.common.mybatis.core.page.TableDataInfo;
import com.haowu.customer.domain.vo.HwAppCustomerVo;
import com.haowu.customer.api.domain.HwAppCustomer;
import com.haowu.customer.domain.bo.HwAppCustomerBo;
import com.haowu.customer.mapper.HwAppCustomerMapper;
import com.haowu.customer.service.IHwAppCustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 用户会员Service业务层处理
 *
 * @author lky
 * @date 2023-02-10
 */
@RequiredArgsConstructor
@Service
public class HwAppCustomerServiceImpl implements IHwAppCustomerService {

    private final HwAppCustomerMapper baseMapper;

    /**
     * 查询用户会员
     */
    @Override
    public HwAppCustomerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户会员列表
     */
    @Override
    public TableDataInfo<HwAppCustomerVo> queryPageList(HwAppCustomerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HwAppCustomer> lqw = buildQueryWrapper(bo);
        Page<HwAppCustomerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户会员列表
     */
    @Override
    public List<HwAppCustomerVo> queryList(HwAppCustomerBo bo) {
        LambdaQueryWrapper<HwAppCustomer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HwAppCustomer> buildQueryWrapper(HwAppCustomerBo bo) {
        LambdaQueryWrapper<HwAppCustomer> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserPhone()), HwAppCustomer::getUserPhone, bo.getUserPhone());
        lqw.eq(bo.getUserSex() != null, HwAppCustomer::getUserSex, bo.getUserSex());
        lqw.eq(bo.getBirthday() != null, HwAppCustomer::getBirthday, bo.getBirthday());
        lqw.eq(StringUtils.isNotBlank(bo.getRegion()), HwAppCustomer::getRegion, bo.getRegion());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HwAppCustomer::getStatus, bo.getStatus());
        lqw.eq(bo.getIsDelete() != null, HwAppCustomer::getIsDelete, bo.getIsDelete());
        return lqw;
    }

    /**
     * 新增用户会员
     */
    @Override
    public Boolean insertByBo(HwAppCustomerBo bo) {
        HwAppCustomer add = BeanUtil.toBean(bo, HwAppCustomer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户会员
     */
    @Override
    public Boolean updateByBo(HwAppCustomerBo bo) {
        HwAppCustomer update = BeanUtil.toBean(bo, HwAppCustomer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HwAppCustomer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户会员
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 校验账号名是否重复
     * @param appCustomer 用户信息
     * @return 结果
     */
    @Override
    public String checkAccountUnique(HwAppCustomer appCustomer) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<HwAppCustomer>()
            .eq(HwAppCustomer::getUserName, appCustomer.getUserName())
            .ne(ObjectUtil.isNotNull(appCustomer.getId()), HwAppCustomer::getId, appCustomer.getId()));
        if (exist) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * app会员注册
     * username即为手机号,同时需要设置默认头像,默认昵称等
     */
    @Override
    public Boolean registerUser(HwAppCustomer appAccount) {
        appAccount.setUserPhone(appAccount.getUserName());
        appAccount.setNickName(DesensitizedUtil.mobilePhone(appAccount.getUserName()));
        appAccount.setUserPath("https://mulanzhijia.oss-cn-shanghai.aliyuncs.com/static/index/art.png");
        appAccount.setUserSex(0);
        //TODO 可选：新用户设置vip等级、赠送积分、新人优惠券等操作
        return baseMapper.insert(appAccount) > 0;
    }
}
